package com.zjitc.controller.rabbittest;

import com.zjitc.utils.rabbit.RabbitMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Create by IntelliJ IDEA
 *
 * @author: lhl
 * @date-Time: 2018/4/30 0030
 * @description:
 */
@Controller
public class RabbitTest {

    @Autowired
    RabbitMessage message;

    @RequestMapping(value = "/testRabbit")
    @ResponseBody
    public String testTabbit(String msg) {
        message.sendMessage(msg);
        return msg;
    }

    @RequestMapping("/LimitUser")
    @ResponseBody
    public String secKill(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        ServletContext context = request.getServletContext();
        Integer counter = null;
        synchronized (context) {
            counter = (Integer) context.getAttribute("counter");
            if (null == counter) {
                counter = new Integer(1);
            } else {
                counter = new Integer(counter.intValue() + 1);
            }
            context.setAttribute("counter", counter);
        }
        return "欢迎第" + counter + "个人";
    }
}
